﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Service4u2.Common;
using Microsoft.Phone.Info;

namespace MoAds.AdAdapters
{
    public abstract class AdAdapter
    {
        protected readonly string BrowserAgentString = "Mozilla/4.0 (compatible; MSIE 7.0; Windows Phone OS 7.0; IEMobile/7.0)";

        #region Public Properties

        /// <summary>
        /// Determines the priority of retrieving ads
        /// </summary>
        public Int32 Priority { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public Int32 AdCount { get; set; }

        #endregion

        /// <summary>
        /// Return the uniqie device id in a UTF-8 format
        /// </summary>
        /// <remarks>
        /// To use this property you must have the Capability "ID_CAP_IDENTITY_DEVICE" set in your WMAppManifest.xml
        /// </remarks>
        protected String DeviceUniqueId
        {
            get
            {
                try
                {
                    Byte[] idArray = DeviceExtendedProperties.GetValue("DeviceUniqueId") as Byte[];
                    return System.Text.Encoding.UTF8.GetString(idArray, 0, idArray.Length);
                }
                catch (UnauthorizedAccessException uaex)
                {
                    Exception ex = new Exception("Ensure the Capability \"ID_CAP_IDENTITY_DEVICE\" is added to the WMAppManifest.xml", uaex);
                    if (ExceptionEvent != null) ExceptionEvent(this, new EventArgs<Exception> { Argument = ex });
                }
                return String.Empty;
            }
        }
        /// <summary>
        /// .
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="Service4u2.Common.EventArgs&lt;System.Exception&gt;"/> instance containing the event data.</param>
        protected void OnException(object sender, EventArgs<Exception> e)
        {
            if (ExceptionEvent != null)
                ExceptionEvent(this, e);
        }


        /// <summary>
        /// 
        /// </summary>
        /// <param name="response"></param>
        protected void OnResult(AdResponse response)
        {
            if (AdResponseEvent != null)
                AdResponseEvent(this, new EventArgs<AdResponse>() { Argument = response });
        }

        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<EventArgs<AdResponse>> AdResponseEvent;

        /// <summary>
        /// 
        /// </summary>
        public event EventHandler<EventArgs<Exception>> ExceptionEvent;

        /// <summary>
        /// 
        /// </summary>
        public abstract void BeginAdRequestAsync();
    }
}
